package org.opengeoingest.tufts.Layer;


import java.util.List;
import java.util.Hashtable;
import org.opengeoingest.tufts.Layer.ToSolr;


import org.campusit.DatabaseTable.DatabaseTable;
import org.campusit.DatabaseTable.ExceptionProcessor;
import org.campusit.DatabaseTable.ExceptionProcessor.Policy;

/**
 * this class manages a sql database table of gis layers.
 * it is built on the DatabaseTable, a Google Code project.  It should use Spring Roo
 * 
 * Since the layer info table is an mssql server database, the config file contains
 * 	SparkDatabaseServer = jdbc:jtds:sqlserver://vm69.atech.tufts.edu/NonESRIGeoportal                                                                                                                                                                           
 *  SparkDatabaseUserName =	someUserName
 *  SparkDatabasePassword =	somePassword
 *  SparkDatabaseConnectionClass = net.sourceforge.jtds.jdbc.Driver
 * more information on connecting to mssql can be found at http://jtds.sourceforge.net/index.html
 * @author smcdon08
 *
 */
public class LayerInfoTable extends DatabaseTable 
{

	//public enum Column {LayerName}
	public enum Column {Name, Title, CatalogName, GeometryType, Abstract, Publisher, Source, 
		North, South, East, West, Fgdc}
	
	public enum GeometryType {Raster, Vector}

	public LayerInfoTable()
	{
		super("LayerInfo", Column.values());
		System.out.println("set mssql type");
		this.sqlType = DatabaseTable.SqlTypeOptions.MSSql;
		//super("Test", Column.values());
	}
	
	
	 @Override
	 protected void initRow(DatabaseRow valuesHash)
	 {

		 valuesHash.put(Column.Name, "");
		 valuesHash.put(Column.Title, "");
		 valuesHash.put(Column.CatalogName, "");
		 valuesHash.put(Column.GeometryType, GeometryType.Vector);
		 valuesHash.put(Column.Abstract, "");
		 valuesHash.put(Column.Publisher, "");
		 valuesHash.put(Column.Source, "");
		 valuesHash.put(Column.North, 0.0);
		 valuesHash.put(Column.South, 0.0);
		 valuesHash.put(Column.East, 0.0);
		 valuesHash.put(Column.West, 0.0);

	     valuesHash.put(Column.Fgdc, "");
	 }
	 
	 /**
	  * return the database row corresponding to the passed name or a new row 
	  * @param name
	  * @return
	  */

	 public DatabaseRow getByName(String name)
	 {
		 DatabaseRow returnValue = select(Column.Name, name);
		 if (returnValue == null)
		 {
			 returnValue = create();
			 returnValue.put(Column.Name, name);
		 }
		 return returnValue;
	 }

	 /**
	  * convert a state object holding layer into to a row in the layer info table
	  * @param values
	  * @return
	  */
	 public static boolean updateFromHash(Hashtable<BaseGeoParser.Key, Object> values)
	 {
		 String name = (String)values.get(BaseGeoParser.Key.EsriName);
		 LayerInfoTable layerInfo = new LayerInfoTable();
		 DatabaseRow row = layerInfo.getByName(name);
		 row.put(Column.Title, values.get(BaseGeoParser.Key.Title));
		 row.put(Column.Fgdc, values.get(BaseGeoParser.Key.FgdcText));
		 row.put(Column.North, values.get(BaseGeoParser.Key.NorthBc));
		 row.put(Column.South, values.get(BaseGeoParser.Key.SouthBc));
		 row.put(Column.East, values.get(BaseGeoParser.Key.EastBc));
		 row.put(Column.West, values.get(BaseGeoParser.Key.WestBc));
		 boolean success = row.dbSave();
		 System.out.println(" saved layer in info table " + success);
		 return success;
	 }
	
	 public static void main(String[] args)
	 {
		 System.out.println("top of InfoTable.main");
		 LayerInfoTable info = new LayerInfoTable();
		 ExceptionProcessor.setPolicy(Policy.Throw);
		 DatabaseRow foo = info.create();
		 System.out.println("foo id = " + foo.getId());
		 foo.put(Column.Fgdc, "test");
		 foo.dbSave();
		 System.out.println("post save foo id = " + foo.getId());
		 //List<DatabaseRow> rows = info.selects();
		 //System.out.println("number of rows = " + rows.size());
	 }
	

}
